<?php


namespace apps\marriage\core\service;


use apps\marriage\core\logic\AuthLogic;
use apps\marriage\core\logic\FileLogic;
use apps\marriage\core\model\MemberAuth;
use apps\marriage\core\model\MemberProfile;
use vm\com\BaseModel;
use vm\com\BaseService;

class MemberAuthService extends BaseService
{

    /**
     * @return MemberAuth
     */
    protected function getModel()
    {
        return new MemberAuth();
    }

    /**
     * @return AuthLogic
     */
    private function getAuthLogic()
    {
        return logic('Auth', LOGIC_NAMESPACE);
    }

    /**
     * @return BaseService|MemberProfileService
     */
    private function getMemberProfileService()
    {
        return service('MemberProfile', SERVICE_NAMESPACE);
    }

    /**
     * @return FileLogic
     */
    private function getFileLogic()
    {
        return logic('File', LOGIC_NAMESPACE);
    }

    public function getByUserId($userId)
    {
        return $this->info(['user_id' => $userId]);
    }

    public function save($data)
    {
        return $this->insert($data, true);
    }

    public function onFaceIdDetectCompleted($userId)
    {
        $detectInfo = $this->getAuthLogic()->getFaceIdDetectInfo($userId);
        if($detectInfo['Text']
            && $detectInfo['Text']['ErrCode'] == 0
            && $detectInfo['Text']['LiveStatus'] == 0
            && $detectInfo['Text']['Comparestatus'] == 0) {
            $data = [
                'user_id' => $userId,
                'name' => $detectInfo['Text']['Name'],
                'idcard' => $detectInfo['Text']['IdCard'],
                'idcard_ocr_front' => $detectInfo['IdCardData']['OcrFront'],
                'idcard_ocr_back' => $detectInfo['IdCardData']['OcrBack'],
                'faceid_detect_status' => 1,
                'faceid_detect_time' => time()
            ];
            $this->insert($data, true);

            $data2 = [
                'realname_auth_status' => 1
            ];
            if($detectInfo['Text']['OcrGender']) {
                $data2['gender'] = $detectInfo['Text']['OcrGender'] == '男' ? MemberProfile::GENDER_MALE : MemberProfile::GENDER_FEMALE;
            }
            if($detectInfo['Text']['OcrBirth']) {
                $data2['birthday'] = strtotime(str_replace('/', '-', $detectInfo['Text']['OcrBirth']) . ' 00:00:00');
            }
            $this->getMemberProfileService()->update($data2, ['user_id' => $userId]);
            return true;
        } else {
            return false;
        }
    }

    public function format($data)
    {
        $data['marriage_auth_status_str'] = '未认证';
        switch ($data['marriage_auth_status']) {
            case MemberAuth::AUTH_STATUS_WAIT:
                $data['marriage_auth_status_str'] = '待审核';
                break;
            case MemberAuth::AUTH_STATUS_REJECT:
                $data['marriage_auth_status_str'] = '认证未通过';
                break;
            case MemberAuth::AUTH_STATUS_APPROVE:
                $data['marriage_auth_status_str'] = '已认证';
                break;
            default:
                break;
        }

        if($data['marriage_auth_file_id'] > 0) {
            $file = $this->getFileLogic()->file($data['marriage_auth_file_id']);
            $data['marriage_auth_file_url'] = $file['url'];
        }

        $data['education_auth_status_str'] = '未认证';
        switch ($data['education_auth_status']) {
            case MemberAuth::AUTH_STATUS_WAIT:
                $data['education_auth_status_str'] = '待审核';
                break;
            case MemberAuth::AUTH_STATUS_REJECT:
                $data['education_auth_status_str'] = '认证未通过';
                break;
            case MemberAuth::AUTH_STATUS_APPROVE:
                $data['education_auth_status_str'] = '已认证';
                break;
            default:
                break;
        }

        if($data['education_auth_file_id'] > 0) {
            $file = $this->getFileLogic()->file($data['education_auth_file_id']);
            $data['education_auth_file_url'] = $file['url'];
        }

        $data['house_auth_status_str'] = '未认证';
        switch ($data['house_auth_status']) {
            case MemberAuth::AUTH_STATUS_WAIT:
                $data['house_auth_status_str'] = '待审核';
                break;
            case MemberAuth::AUTH_STATUS_REJECT:
                $data['house_auth_status_str'] = '认证未通过';
                break;
            case MemberAuth::AUTH_STATUS_APPROVE:
                $data['house_auth_status_str'] = '已认证';
                break;
            default:
                break;
        }

        if($data['house_auth_file_id'] > 0) {
            $file = $this->getFileLogic()->file($data['house_auth_file_id']);
            $data['house_auth_file_url'] = $file['url'];
        }

        $data['car_auth_status_str'] = '未认证';
        switch ($data['car_auth_status']) {
            case MemberAuth::AUTH_STATUS_WAIT:
                $data['car_auth_status_str'] = '待审核';
                break;
            case MemberAuth::AUTH_STATUS_REJECT:
                $data['car_auth_status_str'] = '认证未通过';
                break;
            case MemberAuth::AUTH_STATUS_APPROVE:
                $data['car_auth_status_str'] = '已认证';
                break;
            default:
                break;
        }

        if($data['car_auth_file_id'] > 0) {
            $file = $this->getFileLogic()->file($data['car_auth_file_id']);
            $data['car_auth_file_url'] = $file['url'];
        }

        return $data;
    }
}